/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "inireader.h"

IniReader::IniReader(TarFile *iniFile) : m_activePart(0), m_activePartData(0) {
    if(strcmp(iniFile->filename(), "startup.ini") == 0) {
        char *data = (char *)(iniFile->startPosition());
        char *startPos = data;
        PLRadixTree<char *> *dataPart = 0;
        bool startPart = false;
        while(*data != '\0') {
            if(*data == '[') {
                startPart = true;
                data++;
                startPos = data;
            } else if(*data == ']') {
                startPart = false;
                while(*startPos == ' ') {
                    startPos++;
                }
                char *b = data;
                b--;
                while(*b == ' ') {
                    b--;
                }
                b++;
                char *part = new char[b - startPos + 1];
                memcpy(part, startPos, (b - startPos));
                part[(b - startPos)] = '\0';
                dataPart = new PLRadixTree<char *>();
                m_iniData.insert(part, dataPart);
                data++;
            }
            if(startPart || *data == '\n' || *data == '\r' || *data == ' ') {
                data++;
            } else if(!startPart) {
                startPos = data;
                while(*data != '=') {
                    data++;
                }
                char *b = data;
                b--;
                while(*b == ' ') {
                    b--;
                }
                b++;
                char *part = new char[b - startPos + 1];
                memcpy(part, startPos, (b - startPos));
                part[(b - startPos)] = '\0';
                data++;
                while(*data == ' ') {
                    data++;
                }
                startPos = data;
                while(*data != '\n' && *data != '\r') {
                    data++;
                }
                b = data;
                b--;
                while(*b == ' ') {
                    b--;
                }
                b++;
                char *value = new char[b - startPos + 1];
                memcpy(value, startPos, (b - startPos));
                value[(b - startPos)] = '\0';
                dataPart->insert(part, value);
            }
        }
    }
}

void IniReader::setActivePart(const char *part) {
    PLRadixTree<char *> *_part = m_iniData.lookup(part);
    if(_part) {
        m_activePart = part;
        m_activePartData = _part;
    }
}

const char *IniReader::getValue(const char *key) {
    if(m_activePartData != 0) {
        return m_activePartData->lookup(key);
    }
    return 0;
}
